package com.funkyfurrymonkey.load ;
import openfl.net.URLRequest;
import openfl.events.Event;
import openfl.net.URLLoader;
import openfl.events.IOErrorEvent;

/**
 * ...
 * @author Marcos Martinez
 */
class DataLoader
{

	private var m_cUrlLoader:URLLoader;	
	private var m_sPath:String;
	public function new(sPath:String) 
	{
		m_sPath = sPath;
		m_cUrlLoader = new URLLoader();
		m_cUrlLoader.addEventListener(Event.COMPLETE, OnComplete);
		m_cUrlLoader.addEventListener(IOErrorEvent.IO_ERROR, onError);				
	}
	
	public function start():Void
	{
		m_cUrlLoader.load(new URLRequest(m_sPath));
	}
		
	private function OnComplete(ev:Event):Void
	{
		removeOnFinish();
		onComplete(ev.target);
	}
	
	private function onError(ev:IOErrorEvent):Void
	{
		trace(ev);
		removeOnFinish();
	}
		
	dynamic public function onComplete(target:Dynamic){}
	
	private function removeOnFinish():Void
	{
		m_cUrlLoader.removeEventListener(Event.COMPLETE,OnComplete);
		m_cUrlLoader.removeEventListener(IOErrorEvent.IO_ERROR,onError);
	}
}